from fastapi import APIRouter, Depends, Request, Path

from sqlmodel import Session
from app.admin.deps import get_db
from app.admin.service.system.logs import SystemLogService
from app.admin.service.system.role import SystemRoleService

from app.exception.base import unified_response
from app.admin.service.system.admin import SystemAdminService
from app.admin.service.system.menu import SystemMenuService
from app.models.base import PageListOut
from app.models.system_admin import SystemAdminListIn, SystemAdminOut
from app.models.system_log_login import SystemLogLoginListIn
from app.models.system_log_operate import SystemLogOperateListIn
from app.models.system_menu import SystemMenuCreateIn, SystemMenuEditIn
from app.models.system_role import SystemRoleListIn, SystemRoleAddIn, SystemRoleEditIn, SystemRoleOut

router = APIRouter(prefix="/system")


def get_admin_service(session: Session = Depends(get_db)) -> SystemAdminService:
    return SystemAdminService(session)


def get_role_service(session: Session = Depends(get_db)) -> SystemRoleService:
    return SystemRoleService(session)


def get_menu_service(session: Session = Depends(get_db)) -> SystemMenuService:
    return SystemMenuService(session)


def get_log_service(session: Session = Depends(get_db)) -> SystemLogService:
    return SystemLogService(session)


# 系统管理员
@router.get('/admin/self')
@unified_response
async def admin_self(request: Request,
                     service: SystemAdminService = Depends(get_admin_service)):
    """管理员信息"""
    return await service.get_info(request.state.admin_id)


@router.post("/admin/list", response_model=PageListOut[SystemAdminOut])
@unified_response
async def admin_list(
        list_in: SystemAdminListIn,
        service: SystemAdminService = Depends(get_admin_service),
):
    lists, count = await service.page_list(list_in)
    return PageListOut[SystemAdminOut](lists=lists, count=count, page_no=list_in.page_no, page_size=list_in.page_size)


@router.get("/admin/detail/{admin_id}")
@unified_response
async def admin_detail(
        admin_id: int = Path(..., gt=0, description="ID"),
        service: SystemAdminService = Depends(get_admin_service),
):
    return await service.detail(admin_id)


@router.post("/admin/add")
@unified_response
async def menu_add(
        create_in: SystemMenuCreateIn,
        service: SystemAdminService = Depends(get_admin_service),
):
    return await service.add(create_in)


@router.post("/admin/edit")
@unified_response
async def admin_edit(
        edit_in: SystemMenuEditIn,
        service: SystemAdminService = Depends(get_admin_service),
):
    return await service.edit(edit_in)


@router.post("/admin/delete/{admin_id}")
@unified_response
async def admin_delete(
        admin_id: int = Path(..., gt=0, description="ID"),
        service: SystemAdminService = Depends(get_admin_service),
):
    return await service.delete(admin_id)


# 角色管理
@router.post("/role/list", response_model=PageListOut[SystemRoleOut])
@unified_response
async def role_list(
        list_in: SystemRoleListIn,
        service: SystemRoleService = Depends(get_role_service),
):
    lists, count = await service.page_list(list_in)
    return PageListOut[SystemRoleOut](lists=lists, count=count, page_no=list_in.page_no, page_size=list_in.page_size)


@router.get("/role/detail/{role_id}")
@unified_response
async def role_detail(
        role_id: int = Path(..., gt=0, description="ID"),
        service: SystemRoleService = Depends(get_role_service),
):
    return await service.detail(role_id)


@router.post("/role/add")
@unified_response
async def role_add(
        add_in: SystemRoleAddIn,
        service: SystemRoleService = Depends(get_role_service),
):
    return await service.add(add_in)


@router.post("/role/edit")
@unified_response
async def role_edit(
        edit_in: SystemRoleEditIn,
        service: SystemRoleService = Depends(get_role_service),
):
    return await service.edit(edit_in)


@router.post("/role/delete/{role_id}")
@unified_response
async def role_detail(
        role_id: int = Path(..., gt=0, description="ID"),
        service: SystemRoleService = Depends(get_role_service),
):
    return await service.delete(role_id)


# 菜单管理
@router.get("/menu/routes")
@unified_response
def menu_route():
    """菜单路由"""
    return "menu_route"


@router.get("/menu/all")
@unified_response
async def menu_all_list(
        service: SystemMenuService = Depends(get_menu_service),
):
    return await service.all_list()


@router.get("/menu/list")
@unified_response
async def menu_list(
        service: SystemMenuService = Depends(get_menu_service),
):
    return await service.tree_list()


@router.get("/menu/detail/{menu_id}")
@unified_response
async def menu_detail(
        menu_id: int = Path(..., gt=0, description="ID"),
        service: SystemMenuService = Depends(get_menu_service),
):
    return await service.detail(menu_id)


@router.post("/menu/add")
@unified_response
async def menu_add(
        create_in: SystemMenuCreateIn,
        service: SystemMenuService = Depends(get_menu_service),
):
    return await service.add(create_in)


@router.post("/menu/edit")
@unified_response
async def menu_edit(
        edit_in: SystemMenuEditIn,
        service: SystemMenuService = Depends(get_menu_service),
):
    return await service.edit(edit_in)


@router.post("/menu/delete/{menu_id}")
@unified_response
async def menu_detail(
        menu_id: int = Path(..., gt=0, description="ID"),
        service: SystemMenuService = Depends(get_menu_service),
):
    return await service.delete(menu_id)


# 系统日志
@router.get('/log/operate')
@unified_response
async def log_operate(operate_in: SystemLogOperateListIn = Depends(),
                      service: SystemLogService = Depends(get_log_service)):
    """系统操作日志"""
    return await service.operate(operate_in)


@router.get('/log/login')
@unified_response
async def log_login(login_in: SystemLogLoginListIn = Depends(),
                    service: SystemLogService = Depends(get_log_service)):
    """系统登录日志"""
    return await service.login(login_in)
